package cn.lzm.org.controller;

import cn.lzm.basic.anno.PreAuthorize;
import cn.lzm.basic.util.AjaxResult;
import cn.lzm.basic.util.PageList;
import cn.lzm.org.domain.Department;
import cn.lzm.org.query.DepartmentQuery;
import cn.lzm.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartmentController {


    @Autowired
    private IDepartmentService departmentService;

    /**
     * 添加/修改
     */
    @PostMapping("/addorup")
    @Transactional
    public AjaxResult addOrUpdate(@RequestBody Department department){
        if (null == department.getId()){
            // 添加
            departmentService.add(department);
        }else {
            // 修改
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @PreAuthorize(sn = "department:delete",name = "根据id删除")
    @DeleteMapping("/{id}")
    @Transactional
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.remove(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    public List<Department> findAll(){
        return departmentService.queryAll();
    }

    /**
     * 根据id查询一个
     */

    @GetMapping("/{id}")
    @PreAuthorize(sn = "department:get",name = "根据id查询")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    @PreAuthorize(sn = "department:list",name = "根据id删除")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        System.out.println(query);
        System.out.println(query.getCurrentPage());
        System.out.println(query.getPageSize());
        return departmentService.queryPage(query);
    }
    /*批量删除*/
    @PatchMapping("/batchDel")
    @Transactional
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        System.out.println(ids);
        departmentService.batchDel(ids);
        return AjaxResult.me().success();
    }
    /*上级部门 无限极*/
    @PostMapping("/tree")
    public List<Department> deptTree() {
        List<Department> departments = departmentService.deptTree();
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        System.out.println(departments);
        return departments;
    }

}

